package com.dldata.devtools30.controller.sysform;

import com.alibaba.fastjson.JSON;
import com.dldata.devtools30.entity.DevtFormListtemplOtherBtnEntity;
import com.dldata.devtools30.repository.sysform.DevtFormListtemplOtherBtnDao;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

/**

 */
@Controller
@RequestMapping(value = "/DevtFormListTemplOtherBtnController")
public class DevtFormListTemplOtherBtnController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private DevtFormListtemplOtherBtnDao sysCrfFormListtemplOtherBtnDao;


    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAllSysFormListtemplOtherBtn(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam Map<String, Object> param){


        List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
//        List <SysFormListtemplOtherBtnEntity> customTypeEntityList = sysCrfFormListtemplOtherBtnDao.getAllCloneData();
        List <DevtFormListtemplOtherBtnEntity> customTypeEntityList = Lists.newArrayList(sysCrfFormListtemplOtherBtnDao.findAll());
        customTypeEntityList.stream().filter(
                sysCrfFormListtemplTheaderEntity -> {
                    return sysCrfFormListtemplTheaderEntity.getTempleid().equals(param.getOrDefault("pid", "").toString());
                }
        ).sorted(
                new Comparator<DevtFormListtemplOtherBtnEntity>() {
                    @Override
                    public int compare(DevtFormListtemplOtherBtnEntity o1, DevtFormListtemplOtherBtnEntity o2) {
                        return o1.getXh()-o2.getXh();
                    }
                }
        ).forEach(
                (sysCrfFormTemplateEntity) -> {
                    Map<String, Object> tmpmap = (Map<String, Object>) JSON.parseObject(JSON.toJSONString(sysCrfFormTemplateEntity));
                    tmpmap.put("positionshow","编辑栏");
                    if(sysCrfFormTemplateEntity.getPosition().equalsIgnoreCase("add")){
                        tmpmap.put("positionshow","添加栏");
                    }
                    tmpmap.put("actionshow","弹窗");
                    if(sysCrfFormTemplateEntity.getAction().equalsIgnoreCase("nowin")){
                        tmpmap.put("actionshow","不弹窗");
                    }
                    if(sysCrfFormTemplateEntity.getAction().equalsIgnoreCase("newpage")){
                        tmpmap.put("actionshow","新页面");
                    }
                    tmpmap.put("useflagshow","是");
                    if(sysCrfFormTemplateEntity.getUseflag()==0){
                        tmpmap.put("useflagshow","否");
                    }
                    mapList.add(tmpmap);
                }
        );
        return Result.ok(mapList);
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public Result save(DevtFormListtemplOtherBtnEntity sysCrfFormEntity, HttpServletRequest request, HttpServletResponse response) throws  Exception{

        String result;
        try {
            if (sysCrfFormEntity.getId()==null||sysCrfFormEntity.getId().equals("")) {
                sysCrfFormEntity.setId(UUID.randomUUID().toString());
            }
//          SysFormListtemplOtherBtnEntity entity=  sysCrfFormListtemplOtherBtnDao.save(sysCrfFormEntity.getId(),sysCrfFormEntity);
            DevtFormListtemplOtherBtnEntity entity=  sysCrfFormListtemplOtherBtnDao.save(sysCrfFormEntity);
            if(entity==null){
              return  Result.error("发生异常");
            }
            return Result.ok();

        } catch (Exception ex) {

            logger.error("【执行" + this.getClass().getName() + ".save() 方法失败 】异常：" + ex.getMessage());
            return Result.error("操作错误，请稍候再试！");
        }

    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id,HttpServletRequest request, HttpServletResponse response) throws Exception{
        String result;

        try {
            sysCrfFormListtemplOtherBtnDao.deleteById(id);

            return Result.ok();
        }
        catch (Exception ex){
            logger.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage());
            return Result.error("操作错误，请稍候再试！");
        }

    }

    @RequestMapping(value = "toEdit")
    public String toEdit(String id,String pid, HttpServletRequest request, HttpServletResponse response, Model model) throws  Exception{

        String result;
        try {
            DevtFormListtemplOtherBtnEntity sysCrfFormEntityTmp= new DevtFormListtemplOtherBtnEntity();
            if(!StringUtil.isEmptyOrLength0(id)){
                 sysCrfFormEntityTmp=sysCrfFormListtemplOtherBtnDao.findById(id).get();

            }else{
//                List<SysFormListtemplOtherBtnEntity> alldata = sysCrfFormListtemplOtherBtnDao.getAllCloneData();
                List<DevtFormListtemplOtherBtnEntity> alldata = Lists.newArrayList(sysCrfFormListtemplOtherBtnDao.findAll());
                alldata= alldata.stream()
                        .filter(w->w.getTempleid().equals(pid)).collect(Collectors.toList());
                int xh = alldata.size() + 1;
                for (int i = 0; i < alldata.size(); i++) {
                    DevtFormListtemplOtherBtnEntity entity = alldata.get(i);
                    xh=entity.getXh()>=xh?(entity.getXh()+1):xh;
                }
                sysCrfFormEntityTmp.setTempleid(pid);
                sysCrfFormEntityTmp.setXh(xh);
            }

            model.addAttribute("data",sysCrfFormEntityTmp);
//            model.addAttribute("lxList", searchLxList);
        } catch (Exception ex) {
            logger.error("【执行" + this.getClass().getName() + ".save() 方法失败 】异常：" + ex.getMessage());
        }
        return "thymeleaf/devtools30/sysform/v_SysFormListTemplOtherBtnEdit";
    }

}
